package com.hardydou.data.controller;

import cn.hutool.core.io.IoUtil;
import com.github.pagehelper.PageInfo;
import com.hardydou.common.controller.BaseController;
import com.hardydou.common.result.Result;
import com.hardydou.common.result.ResultAssert;
import com.hardydou.common.result.ResultHelper;
import com.hardydou.data.model.YdIcon;
import com.hardydou.data.model.YdTag;
import com.hardydou.data.service.IYdIconServce;
import com.hardydou.data.service.IYdTagService;
import com.hardydou.framework.log.anno.LogMetaInfo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：hardy
 * @description：
 * @date ：Created in 2020/7/15
 * @Copyright ：Hardydou.com
 */
@Controller
@LogMetaInfo(module = "YdTag")
@RequestMapping(YdTagController.REQ_PREFIX)
public class YdTagController extends BaseController {
    final static String REQ_PREFIX = "/data/yd/tag";

    /***
     * 主模块名称
     * @return
     */
    @Override
    public String getModel() {
        return "data";
    }

    /***
     * 子模块名称
     * @see #page(String)
     * @return
     */
    @Override
    public String getSubModel() {
        return "yd/tag";
    }

    @Autowired
    IYdTagService tagService;

    @GetMapping("")
    public String index() {
        return page("list");
    }

    @ApiIgnore
    @PostMapping("")
    @ResponseBody
    public Result add(YdTag tag) {
        tagService.add(tag);
        return ResultHelper.ok();
    }

    @ApiIgnore
    @PutMapping("")
    @ResponseBody
    public Result edit(YdTag tag) {
        tagService.update(tag);
        return ResultHelper.ok();
    }

    //-----------JSON API---------//
    @ApiOperation(value = "tag列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", defaultValue = "1", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", defaultValue = "10", required = true)
    })
    @GetMapping("/list.json")
    @ResponseBody
    public Result<?> listData(int pageNum, int pageSize, @RequestParam Map params) {
        List<YdTag> list = tagService.list(params);
        PageInfo<YdTag> pageInfo = new PageInfo<>(list);
        return ResultHelper.ok(pageInfo);
    }

    @ApiOperation(value = "删除标签")
    @ApiImplicitParam(name = "tagId", value = "标签ID", required = true)
    @DeleteMapping("")
    @ResponseBody
    public Result del(Long tagId) {
        ResultAssert.notNull(tagId);
        YdTag tag = tagService.findById(tagId);
        ResultAssert.notNull(tag);
        tagService.delete(tagId);
        Result<Map<String, Object>> result = ResultHelper.ok();
        result.getData().put("tagId", tagId);
        result.getData().put("del", true);
        return result;
    }

}
